package com.sakura.gateway.props;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

@Component
public class GatewayAuthProperties implements EnvironmentAware, EnvironmentCapable {

    @Value("${gateway.token.sign-key}")
    @Getter
    private String signKey;

    @Value("${gateway.token.aes-key}")
    @Getter
    private String aesKey;

    @Nullable
    private Environment environment;
    private String env;
    private String name;
    private Boolean isLocal;
    private final Map<String, String> prop;

    public GatewayAuthProperties() {
        this.isLocal = Boolean.FALSE;
        this.prop = new HashMap();
    }

    @Nullable
    public String get(String key) {
        return this.get(key, (String) null);
    }

    @Nullable
    public String get(String key, @Nullable String defaultValue) {
        String value = (String) this.prop.get(key);
        return value == null ? defaultValue : value;
    }

    @Nullable
    public Integer getInt(String key) {
        return this.getInt(key, (Integer) null);
    }

    @Nullable
    public Integer getInt(String key, @Nullable Integer defaultValue) {
        String value = (String) this.prop.get(key);
        return value != null ? Integer.valueOf(value.trim()) : defaultValue;
    }

    @Nullable
    public Long getLong(String key) {
        return this.getLong(key, (Long) null);
    }

    @Nullable
    public Long getLong(String key, @Nullable Long defaultValue) {
        String value = (String) this.prop.get(key);
        return value != null ? Long.valueOf(value.trim()) : defaultValue;
    }

    @Nullable
    public Boolean getBoolean(String key) {
        return this.getBoolean(key, (Boolean) null);
    }

    @Nullable
    public Boolean getBoolean(String key, @Nullable Boolean defaultValue) {
        String value = (String) this.prop.get(key);
        if (value != null) {
            value = value.toLowerCase().trim();
            return Boolean.parseBoolean(value);
        } else {
            return defaultValue;
        }
    }

    @Nullable
    public Double getDouble(String key) {
        return this.getDouble(key, (Double) null);
    }

    @Nullable
    public Double getDouble(String key, @Nullable Double defaultValue) {
        String value = (String) this.prop.get(key);
        return value != null ? Double.parseDouble(value.trim()) : defaultValue;
    }

    public boolean containsKey(String key) {
        return this.prop.containsKey(key);
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public Environment getEnvironment() {
        Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null");
        return this.environment;
    }

    public String getEnv() {
        return this.env;
    }

    public void setEnv(final String env) {
        this.env = env;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public Boolean getIsLocal() {
        return this.isLocal;
    }

    public void setIsLocal(final Boolean isLocal) {
        this.isLocal = isLocal;
    }

    public Map<String, String> getProp() {
        return this.prop;
    }
}

